<template>
  <div class="goods">
    <div v-loading="true" v-if="isLoading"></div>
    <div class="wrap" v-if="">
      <Crumb> </Crumb>
      <img src="../assets/img/banner.4c6b6225.png" width="100%" alt="" />
      <ul class="options">
        <li>
          <strong>排序：</strong>
          <span
            :class="{ active: options1index == index }"
            v-for="(item, index) in options1"
            :key="index"
            @click="options1Fn(index, item.min, item.max)"
            >{{ item.txt }}</span
          >
        </li>
        <li>
          <strong>分类：</strong>
          <span
            :class="{ active: options2index == index }"
            v-for="(item, index) in options2"
            :key="index"
            @click="options2Fn(index, item.type)"
            >{{ item.txt }}</span
          >
        </li>
      </ul>
      <List :arr="GoodsLists" />
      <div v-show="!isReachBottom" class="juzhong wrap">
        <div v-load="isneir"></div>
        <!-- <i class="iconfont icon-loading loading"></i>loading... -->
      </div>
      <div v-show="isReachBottom" class="juzhong wrap">已到底部</div>
    </div>
  </div>
</template>                            
<script>
import Crumb from "../components/Crumb.vue";
import List from "../components/home/List.vue";
import { GoodsSearchAPI } from "@/request/api";
import { getScrollTop, getClientHeight, getScrollHeight } from "@/utils/index";
export default {
  data() {
    return {
      GoodsList: [],
      //优化后的数组
      GoodsLists: [],
      //栏目
      did: 0,
      //实体产品1 虚拟2 全部0
      type: 0,
      //min最小积分
      min: 0,
      //最大积分 如果为0 会忽略其他的逻辑
      max: 10000,
      //关键字
      keyword: "",
      //排序数据
      options1: [
        { min: 0, max: 0, txt: "全部" },
        // "我还可以兑换"的max值是Header组件的coin值
        { min: 0, max: 0, txt: "我还可以兑换" },
        { min: 0, max: 500, txt: "0-500分" },
        { min: 500, max: 1000, txt: "500-1000分" },
        { min: 1000, max: 1500, txt: "1000-1500分" },
        { min: 1500, max: 2500, txt: "1500-2500分" },
        { min: 2500, max: 6500, txt: "2500-6500分" },
        { min: 6500, max: 10000, txt: "6500-10000分" },
      ],
      //高亮
      options1index: 0,
      //分类数据
      options2: [
        { txt: "全部", type: 0 },
        { txt: "实物礼品", type: 1 },
        { txt: "虚拟礼品", type: 2 },
      ],
      //分类高亮
      options2index: 0,
      //图片数
      size: 8,
      //当前页
      page: 1,
      //是否加载数据开关
      isLoading: true,
      //判断是否到底部
      isReachBottom: false,
      //显示
      isimg:false,
      //内容显示
      isneir:false
    };
  },
  created() {
    let keyword = this.$route.query.keyword;
    keyword ||= "";
    this.keyword = keyword;
  setTimeout(()=>{},2000)
    this.goodsSearch();
  },
  mounted() {
  
      
   
  },
  //当路径不变,参数改变时,不会导致组件重载
  //解决:当前页面搜索 无效问题
  watch: {
    "$route.query.keyword": {
      handler(newval, olval) {
        this.keyword = newval;
        this.goodsSearch();
      },
    },
  },
  mounted() {
    window.addEventListener("scroll", this.scrollfn);
  },
  beforeDestroy() {
    window.removeEventListener("scroll", this.scrollfn);
  },
  methods: {
    async goodsSearch() {
      let res = await GoodsSearchAPI({
        did: this.did,
        type: this.type,
        min: this.min,
        max: this.max,
        keyword: this.keyword,
      });
      this.GoodsList = res.data;
      //将数组中的前8条数据存在新的数组中渲染
      this.GoodsLists = this.GoodsList.filter(
        (item, index) => index < this.size
      );
      this.isLoading = false
      // this.isneir=true
      //判断是否加载完毕到
      this.isReachBottom = false;
       this.isneir=false
      this.page = 1;
      if (this.GoodsList.length == this.GoodsLists.length) {
        this.isReachBottom = true;
        return;
      }
    },
    options1Fn(dex, min, max) {
      (this.options1index = dex),
        (this.min = min),
        (this.max = max),
        this.goodsSearch();
    },
    options2Fn(dex, ty) {
      this.type = ty;
      this.options2index = dex;
      this.goodsSearch();
    },
    scrollfn() {
      if (getScrollTop() + getClientHeight() > getScrollHeight() - 240) {
        // 正常情况，将最新的page，size 传递给后台，让后台返回对应数据给我们
        // 积分商城中后台已经把所有数据都给我们了（goodsList）
        // 因此，从 goodsList（0~27） 获取 下一页的数据 push到goodsListShow数组中。
        // 第一页：  page :1 size:8  对应的下标 0~7
        // 第二页：  page: 2 size:8  对应的下标 8~15
        // 第三页：  page: 3 size:8  对应的下标 16~23
        // 第四页：  page: 4 size:8  对应的下标 24~31  24~27
        // （page-1）*size  循环条件：  page*size
        //判断是否加载完毕到
        if (this.GoodsList.length == this.GoodsLists.length) {
          this.isReachBottom = true;
          return;
        }
        if (this.isLoading == false) {
          this.isLoading = true;
                 this.isneir=true
          // console.log("准备加载下一页");
          this.page++;
          setTimeout(() => {
            for (
              let i = (this.page - 1) * this.size;
              i < this.page * this.size;
              i++
            ) {
              // console.log(i);
              this.GoodsList[i] && this.GoodsLists.push(this.GoodsList[i]);
            }
            // console.log(this.GoodsLists);
            this.isLoading = false;
          }, 2000);
        }
      }
    },
  },

  components: {
    Crumb,
    List,
  },
};
</script>
 
<style lang = "less" scoped>
@import "../assets/css/public.less";
.juzhong {
  text-align: center;
}
/* @keyframes rotate {
  100% {
    transform: rotateZ(360deg);
  }
}
@keyframes rotate_ni {
  100% {
    transform: rotateZ(-360deg);
  }
}
.loading{
  animation: rotate_ni 2s linear infinite;
} */
/* .loading {
  animation: go  2s linear alternate;
}
@keyframes go {
  0% {
    transform: rotateZ(180deg);
  }
  50% {
    transform: rotateZ(360deg);
  }

  100% {
    transform: rotateZ(-360deg);
  }
} */

.goods {
  padding-bottom: 50px;
  background: #efefef;
  padding-top: 20px;
  .options {
    padding-top: 20px;
    padding-bottom: 40px;
    li {
      margin-top: 20px;
      strong {
        color: #000;
        font-weight: bold;
      }
      span {
        margin-right: 20px;
        margin-left: 10px;
        cursor: pointer;
        color: #999;
        &.active {
          color: @base-color;
          font-weight: bold;
        }
      }
    }
  }
}
</style>